package com.xywy.beautyand.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xywy.beautyand.MainActivity;
import com.xywy.beautyand.R;

public class MenuFragment extends Fragment implements OnClickListener {
	private View contentView;
	private TextView beautiful_plan, beauterful_ba, count, person;
	private ImageView beautiful_img, beauterful_img, count_img;
	private ImageView person_img;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		contentView = inflater.inflate(R.layout.menu_layout, null);
		return contentView;
	}

	/**
	 * 页面逻辑处理
	 */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initView();
	}

	private void initView() {
		beautiful_plan = (TextView) contentView
				.findViewById(R.id.tv_navi_beauty_plan);
		beauterful_ba = (TextView) contentView
				.findViewById(R.id.tv_navi_beauty_bar);
		count = (TextView) contentView.findViewById(R.id.tv_navi_statistics);
		person = (TextView) contentView.findViewById(R.id.tv_navi_person_info);

		// beautiful_img =
		// (ImageView)contentView.findViewById(R.id.beautiful_img);
		// beauterful_img =
		// (ImageView)contentView.findViewById(R.id.beauterful_img);
		// count_img = (ImageView)contentView.findViewById(R.id.count_img);
		// person_img = (ImageView) contentView.findViewById(R.id.person_img);

		beautiful_plan.setOnClickListener(this);
		beauterful_ba.setOnClickListener(this);
		count.setOnClickListener(this);
		person.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		hideinput();
		String title = "";
		switch (id) {
		case R.id.tv_navi_beauty_plan:// 美美计划
			title = "美美计划";
			beautiful_plan
					.setBackgroundResource(R.color.item_selected_background);
			beauterful_ba.setBackgroundColor(getResources().getColor(
					R.color.transparent));
			count.setBackgroundColor(getResources().getColor(
					R.color.transparent));
			person.setBackgroundColor(getResources().getColor(
					R.color.transparent));
			break;

		case R.id.tv_navi_beauty_bar:// 美吧
			title = "美吧";

			beautiful_plan.setBackgroundColor(getResources().getColor(
					R.color.transparent));
			beauterful_ba
					.setBackgroundResource(R.color.item_selected_background);
			count.setBackgroundColor(getResources().getColor(
					R.color.transparent));
			person.setBackgroundColor(getResources().getColor(
					R.color.transparent));
			break;
		case R.id.tv_navi_statistics:// 统计
			title = "统计";
			beauterful_ba.setBackgroundResource(0);
			beautiful_plan.setBackgroundColor(getResources().getColor(
					R.color.transparent));
			beauterful_ba.setBackgroundColor(getResources().getColor(
					R.color.transparent));
			count.setBackgroundResource(R.color.item_selected_background);
			person.setBackgroundColor(getResources().getColor(
					R.color.transparent));
			break;

		case R.id.tv_navi_person_info:// 个人中心
			title = "个人信息";
			beautiful_plan.setBackgroundColor(getResources().getColor(
					R.color.transparent));
			beauterful_ba.setBackgroundColor(getResources().getColor(
					R.color.transparent));
			count.setBackgroundColor(getResources().getColor(
					R.color.transparent));
			person.setBackgroundColor(getResources().getColor(
					R.color.item_selected_background));
			break;
		}
		switchFragment(id, title);
	}

	private void switchFragment(int id, String title) {
		if (getActivity() == null)
			return;
		if (getActivity() instanceof MainActivity) {
			MainActivity main = (MainActivity) getActivity();
			main.setFragment(title, id);
		}
	}

	private void hideinput() {
		if (getActivity().getCurrentFocus() != null) {
			((InputMethodManager) getActivity().getSystemService(
					Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
					getActivity().getCurrentFocus().getWindowToken(),
					InputMethodManager.HIDE_NOT_ALWAYS);
		}

	}
}
